<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="add" v-if="isShow(179)">新 增</el-button>       
      </el-col>
    </el-row>
    
     <el-row class="myMarginTop_10px" :gutter="10">     
      <el-col :span="3">
        <template>
          <el-select 
            v-model="CATEGORY_ID2"
            clearable
            @change="onGetJumpValue"
            placeholder="所有二级分类">
            <el-option
              v-for="item in twoList"
              :key="item.CATEGORY_ID"
              :label="item.CATEGORYNAME"
              :value="item.CATEGORY_ID">
            </el-option>
          </el-select>
        </template>
      </el-col>
    
      <el-col :span="3">
        <el-select v-model="CATEGORY_ID3"
        	placeholder="所有三级分类"
        	clearable
          >
          <el-option
            
                   v-for="item in jumpValueList"
                   :key="item.CATEGORY_ID"
                   :label="item.CATEGORYNAME"
                   :value="item.CATEGORY_ID">
          </el-option>
        </el-select>
      </el-col>
       <el-col :span="8">
        <el-date-picker
          v-model="time"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-col>
       <el-col :span="5">
        <el-input placeholder="请输入关键词" v-model="kw"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    
    <el-row class="myMarginTop_10px">
      <el-col :span="24">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="list"
          :border = true
          :stripe = true
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            prop="GOODS_ID"
            width="55">
          </el-table-column>
         
          <el-table-column
            prop="CATEGORYNAME"
            label="分类名称"
            >          
          </el-table-column>
          
           <el-table-column
            prop="STORENAME"
            label="店铺名称"
            >          
          </el-table-column>
          
          <el-table-column
            prop="GOODSNAME"
            label="商品名称"
            >
          </el-table-column>
          
          <el-table-column
            prop="BARCODE"
            label="商品条码"
            >
          </el-table-column>
          
          <el-table-column
            prop="LOGOIMG"
            label="商品图片"
            >
            <template scope="scope">
                <img :src='EBIZ_SHOW_PIC_API+scope.row.LOGOIMG' width="50" height="50"/>                
            </template>
          </el-table-column>
          
          <el-table-column
            prop="PRICE"
            label="销售价格"
            >
          </el-table-column>


          <el-table-column
            prop="AMOUNT"
            label="成团人数"
            >
          </el-table-column>
          
          <el-table-column
            label="已团人数"
            prop="SOLDCOUNT"
            >
          </el-table-column>

          <el-table-column
            prop="STAR_TIME"
            label="开团时间"
            >
          </el-table-column>
          
          <el-table-column
            prop="END_TIME"
            label="闭团时间"
            >
          </el-table-column>
          
          <el-table-column
            prop="ISONSALE"
            label="是否上架"
            >
             <template scope="scope">
                    <span>{{scope.row.ISONSALE | ISONSALEType}}</span>
                  </template>
          </el-table-column>
          
          <el-table-column
            prop="DELETED"
            label="删除标记"
            >
             <template scope="scope">
              <span v-if="scope.row.DELETED==0">未删除</span>
              <span v-else class="myColorRed">已删除</span>
            </template>
          </el-table-column>
                   
          <el-table-column
            label="操作"
            width="200">
            <template scope="scope">     
              <span v-if="scope.row.GOODSSTATUS==0">
                <el-button type="text" @click="xiajia(scope.row.GOODS_ID,'上架')" class="modifly" size="small" v-if="isShow(180)">上架</el-button> 
                <el-button type="text" @click="modify(scope.row.GOODS_ID)" class="modifly" size="small" v-if="isShow(181)">| 编辑</el-button> 
              </span>
              <span v-if="scope.row.GOODSSTATUS==1">
                <el-button type="text" @click="xiajia(scope.row.GOODS_ID,'下架')" class="modifly" size="small">下架</el-button> 
                <el-button type="text" @click="modify(scope.row.GOODS_ID)" class="modifly" size="small" v-if="isShow(181)">| 编辑</el-button> 
              </span>
              <span v-if="scope.row.GOODSSTATUS==2">
                团购进行时
              </span>
              <span v-if="scope.row.GOODSSTATUS==3">
                团购已结束
              </span>
            </template>
          </el-table-column>
        </el-table>

        <div class="block">                    
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page_num"
            :page-sizes="[10, 15]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
    padding-bottom: 5px;
  }
  .red{
  	color:#fff;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
   .myColorRed{
    color: #f00;
  }
</style>

<script>
	import { EBIZ_SHOW_PIC_API } from '@/config/baseUrl';
  import { getCommodityList,hot,best,onsale,twot_o,soldout,deletethis,getJumpValue , getTwoList } from '@/api/commodity';
  import qs from "querystring";
  
  import { extractAreaIds } from '@/utils/misc';
  import { getRegion } from '@/utils/localstorage';

  export default {
    data(){
      return{  
        title:"团购商品管理",    
        list:[],  

        total:null, //总条数  
        page_num: 1, //当前页码
        page_size: 10, //每页显示多少条            
      
        id:null,
        index:null,
        idArr:[],
        area_id: -1,
        pages:1000,
        loading:false,
        multipleSelection: [],        
        
        EBIZ_SHOW_PIC_API:EBIZ_SHOW_PIC_API,
        kw:"",
                    
        is_modifly:false,
        twot_o:twot_o,
        CATEGORY_ID2:"",
        CATEGORY_ID3:"",
        hot:hot,
        ISHOT:'',
        best:best,
        ISBEST:"",
        onsale:onsale,
        ISONSALE:"",
        start:"",
        end:"",
 
        jumpValueList:[],
        time:[],
        twoList:[]
      }
    },
    
    created() {
      document.title=this.title;
      this.onGetTwoList();
      this.fetchList();
//    this.regionOptions = getRegion();
//    this.regionOptSelected.push(this.regionOptions[0].value);
    },

    filters: {
      ISONSALEType: function(value) {     
        let src = '';
        switch (value) {
          case 0:
            src = '否';
          break;
          case 1:
            src = '是';
          break;                
        }
        return src;    
      },
      
      ISBESTType: function(value) {     
        let src = '';
        switch (value) {
          case 0:
            src = '否';
          break;
          case 1:
            src = '是';
          break;
                
        }
        return src;    
      },
      
      ISHOTType: function(value) {     
        let src = '';
        switch (value) {
          case 0:
            src = '否';
          break;
          case 1:
            src = '是';
          break;
                
        }
        return src;    
      }
  },
   
    methods:{
      onGetTwoList(){
        let vm=this;
        let params={
          CATEGORYTYPE:3,
          PARENTID:3,
          DELETED:0,
          CATEGORYGRADE:2
        }
        getTwoList(qs.stringify(params)).then(response => {
          let res=response.data;
          vm.twoList=res.OBJECT;
          // console.log(res)
        }).catch(error => {
          console.log(error)
        })
      },
      fetchList() {
        let vm = this;       
        if(vm.time){
          vm.start=this.time[0];
          vm.end=this.time[1];
        }else{
          vm.start="";
          vm.end="";
        }
        const params = {
//        storetype: 2,
          page: vm.page_num,
          rows: vm.page_size,
          GOODSTYPE: 3,
          CATEGORY_ID2:this.CATEGORY_ID2,
          CATEGORY_ID3:this.CATEGORY_ID3,
          start:this.start,
          end:this.end,
          GOODSNAME:encodeURI(vm.kw)                 
        }
        vm.loading = true;
        getCommodityList(params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;       
          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        });
      },
      
       xiajia(GOODS_ID,e){//上架
        let vm=this;
        vm.$confirm('您确定要'+e+'该商品吗？', '提示', {//弹出框
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params={
            GOODS_ID:GOODS_ID,
            type:2
          }
          soldout(qs.stringify(params)).then(response => {//逻辑删除商品
            vm.fetchList();
            vm.$message({
              type: 'success',
              message: response.data.MSG
            });
          }).catch(error => {
            vm.$message.error("执行失败！");
          })
        }).catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消执行'
          });          
        });
      },
      
      fuzzySearch() {
         this.fetchList();
      },
      
      handleSizeChange(val) {
        this.page_size = val;
        this.fetchList();
      },
      
      handleCurrentChange(val) {
        this.page_num = val;
        this.fetchList();
      },

      add:function(){
        this.$router.push({
          path:'/purchaseCommodityComonents',
          query: {type: 'add'}
        })
      },
      
      modify:function(ADID){
        this.$router.push({
          path:'/purchaseCommodityComonents',
          query: {type: 'update', id: ADID}
        })
      },

      onGetJumpValue(val){//动态拉取跳转值
        let vm=this,goodstype=val;                          
          let params={
            CATEGORYTYPE:3,
            CATEGORYGRADE:3,
            DELETED:0,
            PARENTID:vm.CATEGORY_ID2
          }
          getJumpValue(params).then(response => {
            let res=response.data.OBJECT;
            vm.CATEGORY_ID3="";
            vm.jumpValueList=res;
            // console.log("拉取成功",res);
          }).catch(error => {
            console.log("拉取失败",error);
          })    
      },  
      
      //-------删除
        deleteData(id){//删除
        let vm=this;
        vm.$confirm('您确定要删除这条数据吗？', '提示', {//弹出框
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params={
          	type:1,
            GOODS_ID:id
          }
          deletethis(qs.stringify(params)).then(response => {//请求删除接口
            vm.$message({
              type: 'success',
              message: response.data.MSG
            });
            vm.fetchList();
          }).catch(error => {
            vm.$message.error("执行失败！");
          });
        }).catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消执行'
          });          
        });
      },
      
     //----页码---
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].area_id)
          }
        }else{
          this.idArr = [];
        }            
      },
      
       indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      }
    }

  }


</script>



